{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**交换数组arr中的列1和列3。**\n", "\n", "- `arr = np.arange(9).reshape(3, 3)`\n", "\n", "【知识点:索引与切片】\n", "- 如何交换二维数组中的两列?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:56:51.266868Z", "start_time": "2020-09-06T02:56:51.260884Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]\n", " [6 7 8]]\n", "[[2 1 0]\n", " [5 4 3]\n", " [8 7 6]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arr = np.arange(9).reshape(3, 3)\n", "print(arr)\n", "# [[0 1 2]\n", "# [3 4 5]\n", "# [6 7 8]]\n", "\n", "x = arr[:, [2, 1, 0]]\n", "print(x)\n", "# [[2 1 0]\n", "# [5 4 3]\n", "# [8 7 6]]" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:57:26.869431Z", "start_time": "2020-09-06T02:57:26.864408Z" } }, "source": [ "**交换数组arr中的第1行和第2行。**\n", "- `arr = np.arange(9).reshape(3, 3)`\n", "\n", "【知识点:索引与切片】\n", "- 如何交换二维数组中的两行?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:57:43.336029Z", "start_time": "2020-09-06T02:57:43.331042Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]\n", " [6 7 8]]\n", "[[3 4 5]\n", " [0 1 2]\n", " [6 7 8]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arr = np.arange(9).reshape(3, 3)\n", "print(arr)\n", "# [[0 1 2]\n", "# [3 4 5]\n", "# [6 7 8]]\n", "\n", "x = arr[[1, 0, 2], :]\n", "print(x)\n", "# [[3 4 5]\n", "# [0 1 2]\n", "# [6 7 8]]" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:58:04.059418Z", "start_time": "2020-09-06T02:58:04.054430Z" } }, "source": [ "**反转二维数组arr的行。**\n", "\n", "- `arr = np.arange(9).reshape(3, 3)`\n", "\n", "【知识点:索引与切片】\n", "- 如何反转二维数组的行?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:58:21.071694Z", "start_time": "2020-09-06T02:58:21.067682Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]\n", " [6 7 8]]\n", "[[6 7 8]\n", " [3 4 5]\n", " [0 1 2]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arr = np.arange(9).reshape(3, 3)\n", "print(arr)\n", "# [[0 1 2]\n", "# [3 4 5]\n", "# [6 7 8]]\n", "\n", "x = arr[::-1, :]\n", "print(x)\n", "# [[6 7 8]\n", "# [3 4 5]\n", "# [0 1 2]]" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:58:39.880250Z", "start_time": "2020-09-06T02:58:39.875230Z" } }, "source": [ "**反转二维数组arr的列。**\n", "- `arr = np.arange(9).reshape(3, 3)`\n", "\n", "【知识点:索引与切片】\n", "- 如何反转二维数组的列?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:58:54.851622Z", "start_time": "2020-09-06T02:58:54.846601Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]\n", " [6 7 8]]\n", "[[2 1 0]\n", " [5 4 3]\n", " [8 7 6]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arr = np.arange(9).reshape(3, 3)\n", "print(arr)\n", "# [[0 1 2]\n", "# [3 4 5]\n", "# [6 7 8]]\n", "\n", "x = arr[:, ::-1]\n", "print(x)\n", "# [[2 1 0]\n", "# [5 4 3]\n", "# [8 7 6]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python35", "language": "python", "name": "python35" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }